package com.xjczjj.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjczjj.model.common.Result;
import com.xjczjj.model.pojo.Permission;
import com.xjczjj.model.pojo.SysUser;
import com.xjczjj.model.pojo.UserInfo;
import com.xjczjj.model.qo.UserQueryQo;
import com.xjczjj.model.vo.RouterVo;
import com.xjczjj.service.SysUserService;
import com.xjczjj.utils.MenuTree;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 管理员表	 前端控制器
 * </p>
 *
 * @author 朱佳吉
 * @since 2024-06-05 11:39:11
 */
@RestController
@RequestMapping("api/sysUser")
public class SysUserController {
    /**
     * 获取菜单数据
     */
    @Autowired
    private SysUserService sysUserService;
    @GetMapping("/getMenuTree")
    public Result getMenuTree() {
        int loginIdAsInt = StpUtil.getLoginIdAsInt();
        SysUser sysUser = sysUserService.getById(loginIdAsInt);
        List<Permission> permissionList = sysUser.getPermissionList();
        // 筛选目录和菜单
        List<Permission> collect = permissionList.stream().filter(item -> item != null && (item.getType() == 0 || item.getType() == 1))
                .collect(Collectors.toList());
        // 生成路由数据
        List<RouterVo> routerVos = MenuTree.makeRouter(collect, 0L);
        return Result.ok(routerVos).message("菜单数据获取成功");
    }

    /**
     * 获取登录用户信息
     * @return
     */
    @GetMapping("/getUserInfo")
    public Result getUserInfo() {
        int loginIdAsInt = StpUtil.getLoginIdAsInt();
        SysUser sysUser = sysUserService.getById(loginIdAsInt);
        List<Permission> permissionList = sysUser.getPermissionList();
        Object[] roles = permissionList.stream().map(Permission::getCode).toArray();
        UserInfo userInfo = new UserInfo();
        BeanUtils.copyProperties(sysUser, userInfo, "password");
        userInfo.setUserRoleResp(userInfo.getUserRoleResp());
        return Result.ok().message("用户信息查询成功");

    }

    /**
     * 分页查询
     * @param userQueryQo
     * @return
     */
    @GetMapping("/list")
    public Result list(UserQueryQo userQueryQo) {
        IPage<SysUser> page = new Page<SysUser>(userQueryQo.getPageNum(), userQueryQo.getPageSize());
        sysUserService.findUserListByPage(page,userQueryQo);
        return Result.ok(page);
    }

    /**
     * 新增用户信息
     * @param sysUser
     * @return
     */
    @PostMapping("/save")
    public Result saveSysUser(@RequestBody SysUser sysUser) {
        // todo: 新增用户信息
        sysUserService.save(sysUser);
        return null;
    }

    /**
     * 更新用户信息
     * @param sysUser
     * @return
     */
    @PostMapping("/updateSysUser")
    public Result updateSysUser(@RequestBody SysUser sysUser) {
        // todo: 更新用户信息
        if (sysUserService.updateById(sysUser)) {
            return Result.ok().message("更新用户信息成功");
        }
        return Result.error().message("更新用户信息失败");
    }

    /**
     * 批量删除用户信息
     * @param ids 系统用户id字符串，以逗号分割
     * @return
     */
    @DeleteMapping("/delete")
    public Result deleteSysUser(@RequestBody String ids) {
        // todo: 批量删除用户信息
        String[] split = ids.split(",");
        List<Long> collect = java.util.Arrays.stream(split).map(Long::parseLong).collect(Collectors.toList());
        if (sysUserService.removeByIds(collect)) {
            return Result.ok().message("批量删除用户信息成功");
        }
        return Result.error().message("批量删除用户信息失败");
    }

}
